package com.lzh.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


//import com.lzh.base.acion.BaseAction;
import com.lzh.model.UserBean;
import com.lzh.service.UserService;
@Controller
public class UserAction {
	@Autowired
	private UserService userService;
	
	
	

	 //输入是什么类型，就在形参里写什么类型。
	//输出是什么类型，就在service层写什么类型。
    
    
	 //======================================================================
    
    //显示所有数据
    @RequestMapping("/userList")
    public String showAll(HttpServletRequest request){
    	System.out.println("index......");   	
    	List<UserBean> userList = new ArrayList<UserBean>();
    	userList = userService.getUsers();
    	if(userList!=null)System.out.println("bukong");
    	request.setAttribute("userList", userList);
    	System.out.println(userList.get(0).getID());  
    	System.out.println(userList.get(0).getTitle()); 
    	System.out.println(userList.get(0).getDate()); 
    	return "login";
    }
    
    
    
    
   
    //======================================================================
    
    //添加数据
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(UserBean user,HttpServletResponse response) throws IOException{ 
		
		Date date = new Date();
		user.setDate(date);
		//自动封装，user.setTitle（）；。。。。。
		//自动封装，user.setContext（）；。。。。。
		//自动封装，user.setPerson（）；。。。。。
		System.out.println(user.getPerson()+"自动封装了javaBean");
		if(userService.insertUser(user)==1){
			System.out.println("添加一条");
			
			response.sendRedirect("login.jsp?aSuc=1");

		}
	
	    
	}
	
	
	
	
	   //======================================================================
		//查找数据
		@RequestMapping(value="/find",method=RequestMethod.POST)  
		public String findUser(String person,HttpServletRequest request,HttpServletResponse response) throws Exception {  
			List<UserBean> userList = new ArrayList<UserBean>();
			userList=userService.getUserInfo(person);
			request.setAttribute("userList", userList);
			
			if(userList.size() == 0){
				System.out.println("通过判断……");  
				response.sendRedirect("login.jsp?err=1");
			}
//	    	
			return "login";
		}
	
	
		//======================================================================
		//删除数据
		@RequestMapping(value="/deleteuser")
		//@用注解@RequestParam直接获取ID
		public void deleteuser(@RequestParam(value="ID") int ID,HttpServletResponse response) throws IOException{
		
			if(userService.deleteUser(ID)==1){
				response.sendRedirect("login.jsp?dSuc=1");
			}
			
			
			
		}
	
	
	
	
	
		//======================================================================
	    //修改数据
	
	@RequestMapping(value="/updateuser",method=RequestMethod.POST)  
	public String updateUser( UserBean userInfo ) throws Exception {  
		System.out.println("进入修改controller部分……");
		Date date = new Date();
		userInfo.setDate(date);
		userService.updateUser(userInfo);
		System.out.println("修改成功……");
		return "login";
	}
}
